﻿using EFCoreRepository.Extensions;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RescueApp.Data;
using RescueApp.Models;
using Rescue.UI.Common;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using Rescue.Services;

namespace RescueApp.UI
{
    public partial class RescueApplyUpdate : Form
    {
        private RescueContext db;
        private RescueApply rescueApply;
        private RescueApplyService srv;

        public RescueApplyUpdate()
        {
            InitializeComponent();
        }

        public RescueApplyUpdate(RescueContext db, RescueApplyService srv, RescueApply rescueApply)
        {
            InitializeComponent();
            this.db = db;
            this.rescueApply = rescueApply;
            this.srv = srv;
        }

        private bool ValidateItem()
        {
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("故障车车牌不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (string.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("故障车型不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (string.IsNullOrEmpty(textBox4.Text))
            {
                MessageBox.Show("驾驶证不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (string.IsNullOrEmpty(textBox5.Text))
            {
                MessageBox.Show("申请人电话不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (string.IsNullOrEmpty(textBox6.Text))
            {
                MessageBox.Show("故障地点不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (string.IsNullOrEmpty(textBox7.Text))
            {
                MessageBox.Show("维修目的地不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (string.IsNullOrEmpty(textBox8.Text))
            {
                MessageBox.Show("申请日期不能为空！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (!DateTime.TryParse(textBox8.Text, out _))
            {
                MessageBox.Show("申请日期格式不正确，请输入有效的日期格式（例如：yyyy-MM-dd）！", "验证失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        //初始化时写入所选ID的信息
        private void ChangeRescueApply_Load_1(object sender, EventArgs e)
        {
            textBox1.Text = rescueApply.RescueID.ToString();
            textBox2.Text = rescueApply.LicenseID;
            textBox3.Text = rescueApply.CarType;
            textBox4.Text = rescueApply.DriverLicenseID;
            textBox5.Text = rescueApply.Phone;
            textBox6.Text = rescueApply.RescueLocation;
            textBox7.Text = rescueApply.Destination;
            textBox8.Text = rescueApply.FillingDate.ToString();

            textBox1.ReadOnly = true;
            textBox1.BackColor = SystemColors.Control;
        }

        private void button1_Click(object sender, EventArgs e)//保存按钮
        {
            if (!ValidateItem()) return;
            rescueApply.LicenseID = textBox2.Text.Trim();
            rescueApply.CarType = textBox3.Text.Trim();
            rescueApply.DriverLicenseID = textBox4.Text.Trim();
            rescueApply.Phone = textBox5.Text.Trim();
            rescueApply.RescueLocation = textBox6.Text.Trim();
            rescueApply.Destination = textBox7.Text.Trim();
            rescueApply.FillingDate = DateTime.Parse(textBox8.Text.Trim());

            var entity = db.RescueApplies.FirstOrDefault(x => x.RescueID == rescueApply.RescueID);
            if (entity != null)
            {

                entity.LicenseID = rescueApply.LicenseID;
                entity.CarType = rescueApply.CarType;
                entity.DriverLicenseID = rescueApply.DriverLicenseID;
                entity.Phone = rescueApply.Phone;
                entity.RescueLocation = rescueApply.RescueLocation;
                entity.Destination = rescueApply.Destination;
                entity.FillingDate = rescueApply.FillingDate;


                try
                {
                    srv.Update(entity);
                    //db.SaveChanges();
                    MessageBox.Show("修改成功!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)//取消按钮
        {
            textBox2.Text = rescueApply.LicenseID;
            textBox3.Text = rescueApply.CarType;
            textBox4.Text = rescueApply.DriverLicenseID;
            textBox5.Text = rescueApply.Phone;
            textBox6.Text = rescueApply.RescueLocation;
            textBox7.Text = rescueApply.Destination;
            textBox8.Text = rescueApply.FillingDate.ToString();
        }
    }

}

